package com.cennavi.bigdata.baotou.controller;

import com.alibaba.fastjson.JSONObject;
import com.cennavi.bigdata.baotou.service.ImportService;
import com.cennavi.bigdata.baotou.service.ViewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * Created by cennavi on 2018/10/23.
 */
@Controller
@RequestMapping("/import")
public class ImportController {

    @Autowired
    private ImportService importService;

    @Autowired
    private ViewService viewService;

    @ResponseBody
    @RequestMapping(value = "/importAnalyticalDecision")
    public String importAnalyticalDecision(@RequestParam(value = "file") MultipartFile file){

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("errcode",0);
        //1基础路段信息导入数据库
        //2整合、切割后的路段信息导入数据库
        //3整合、切割后的路段id跟基础路段id对应关系入库
        try {
            int roadCount = importService.saveRoad_Piecewise_Data(file);
            String road_newCount = importService.saveRoad_Piecewise_Data_New_T(file);
            jsonObject.put("errmsg","基础路段入库数量"+roadCount+"条！切分、整合后的路段入库"+road_newCount.split(",")[0]+"条！路段道路对应表插入数据"+road_newCount.split(",")[1]+"条！");
        }catch (Exception e){
            e.printStackTrace();
        }
        return  jsonObject.toString();
    }


    /**
     * 导入坐标数据
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "insertToPgRoadGeomtry",method = RequestMethod.GET,produces = "text/plain;charset=UTF-8")
    public String insertToPgRoadGeomtry(){

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("errcode",0);
        try {
            List<Map<String,Object>> list = viewService.insertToPgRoadGeomtry();
            int count = importService.insertToPgRoadGeomtry(list);
            jsonObject.put("errmsg","数据保存成功，共"+count+"条");
        }catch (Exception e){
            e.getMessage();
            jsonObject.put("errcode",-1);
            jsonObject.put("errmsg","数据获取异常");
        }
        return jsonObject.toString();
    }


}
